import data_control from "../../../../engines/libs/data_control";
import levelAvoidBase from "../levelAvoidBase";
import levelAvoidPlayer from "./levelAvoidPlayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class levelAvoidEnemy extends cc.Component {
    @property({ displayName: "一倍大小下字体大小" })
    scaleLabFontSize: number = 40;
    private player: levelAvoidPlayer = null;
    private moveData: EnemyMoveData = null
    private levelbase: levelAvoidBase = null
    isMove: boolean = false;
    private time = 0;
    lab: cc.Label = null;
    onLoad() {
        this.lab = this.node.getChildByName("lab").getComponent(cc.Label)
    }
    targetIndex: number = 0;
    targetPos: cc.Vec2 = null
    protected update(dt: number): void {
        if (!this.moveData) return
        if (!this.isMove) return;
        if (!this.startBorn) return;
        if (this.levelbase.isPause) return;
        if (this.levelbase.openTips) return;
        // this.time += dt;
        // if (this.time < 0.016) return;

        // this.time = 0;
        this.breath()
        switch (this.moveData.moveType) {
            case EnemyMoveType.one:
                let pos = this.node.getPosition().add(this.player.node.getPosition().sub(this.node.getPosition()).normalize().mul(this.moveData.moveSpeed * dt / 0.016))
                this.node.setPosition(pos)
                break;
            case EnemyMoveType.two:
                let postwo = this.node.getPosition().add(this.moveData.moveDirection.mul(this.moveData.moveSpeed * dt / 0.016))
                this.node.setPosition(postwo)
                break;
            case EnemyMoveType.three:
                let mag = this.player.node.getPosition()

                if (Math.abs(mag.y - this.moveData.startPos.y) < this.moveData.attRanglePos.y && Math.abs(mag.x - this.moveData.startPos.x) < this.moveData.attRanglePos.x && !this.player.isDeathIng) {

                    let pos = this.node.getPosition().add(this.player.node.getPosition().sub(this.node.getPosition()).normalize().mul(this.moveData.moveSpeed * dt / 0.016))
                    this.node.setPosition(pos)
                } else {
                    let startPosMag = this.moveData.startPos.sub(this.node.getPosition())
                    let pos = this.node.getPosition().add(startPosMag.normalize().mul(this.moveData.moveSpeed * dt / 0.016))

                    if (startPosMag.mag() < this.moveData.moveSpeed * dt / 0.016) pos = this.moveData.startPos
                    this.node.setPosition(pos)
                }
                break;

            case EnemyMoveType.four:
                if (this.moveData.targetPos[this.targetIndex] == null) break;
                let targetMag = this.moveData.targetPos[this.targetIndex].sub(this.node.getPosition())
                let targetpos = this.node.getPosition().add(targetMag.normalize().mul(this.moveData.moveSpeed * dt / 0.016))

                if (targetMag.mag() < this.moveData.moveSpeed * dt / 0.016) {
                    targetpos = this.moveData.targetPos[this.targetIndex]
                    this.targetIndex++;
                    if (this.targetIndex >= this.moveData.targetPos.length) {
                        this.targetCall && this.targetCall();
                        // this.targetCall = null;
                        this.targetIndex = 0
                        break;
                    }
                }
                // console.error(this.node.y,targetpos.x,targetpos.y)
                this.node.setPosition(targetpos)

                break;

            case EnemyMoveType.five:
                if (this.targetPos == null) {
                    this.targetPos = cc.v2(
                        data_control.getRandNumberOfArrayTwo([this.moveData.rangleV4.x, this.moveData.rangleV4.y]),
                        data_control.getRandNumberOfArrayTwo([this.moveData.rangleV4.z, this.moveData.rangleV4.w])
                    )
                }
                let fivetargetMag = this.targetPos.sub(this.node.getPosition())
                let fivetargetpos = this.node.getPosition().add(fivetargetMag.normalize().mul(this.moveData.moveSpeed))

                if (fivetargetMag.mag() < this.moveData.moveSpeed) {
                    // fivetargetpos = this.targetPos
                    this.targetPos = cc.v2(
                        data_control.getRandNumberOfArrayTwo([this.moveData.rangleV4.x, this.moveData.rangleV4.y]),
                        data_control.getRandNumberOfArrayTwo([this.moveData.rangleV4.z, this.moveData.rangleV4.w])
                    )

                }
                this.node.setPosition(fivetargetpos)

                break;
            case EnemyMoveType.six:
                if (this.berziTime == null) this.berziTime = dt;
                else this.berziTime += dt;
                let targetPos: cc.Vec2 = null
                if (this.berziTime > this.moveData.beiziTime) targetPos = this.moveData.routePosList[this.moveData.routePosList.length - 1];
                else {
                    targetPos = data_control.getBerziPos(this.moveData.routePosList, this.berziTime / this.moveData.beiziTime)
                }
                this.node.setPosition(targetPos)
                break;
        }
    }
    berziTime: number = null;
    startBorn = false;
    public init(showlab: string, player: levelAvoidPlayer, moveData: EnemyMoveData, levelbase: levelAvoidBase, scaleNum: number = 1, needBornAni: boolean = true) {
        showlab && (this.lab.string = showlab);
        this.levelbase = levelbase;
        this.player = player;
        this.moveData = moveData;
        if (moveData.startPos) this.node.setPosition(moveData.startPos)

        // this.scheduleOnce(() => {
        //     this.node.width = this.lab.node.width;
        //     this.node.height = this.lab.node.height;
        //     this.node.getComponent(cc.BoxCollider).size.width = this.node.width
        //     this.node.getComponent(cc.BoxCollider).size.height = this.node.height
        // })
        if (needBornAni) {
            this.scale = 0;

            cc.tween(this.node.getComponent(levelAvoidEnemy))
                .to(0.3, { scale: scaleNum })
                .call(() => {
                    this.startBorn = true;
                })
                .start()


            // this.levelbase.scheduleAny(() => {
            //     this.scale += scaleNum / 0.3;

            // })
        } else {
            this.scale = scaleNum;
            this.startBorn = true;
            this.scheduleOnce(() => {
                this.node.width = this.lab.node.width;
                this.node.height = this.lab.node.height;
                this.node.getComponent(cc.BoxCollider).size.width = this.node.width - 40;
                this.node.getComponent(cc.BoxCollider).size.height = this.node.height - 40;
            })
        }

        this.initScale = scaleNum;
    }
    public startAvoid(move: boolean = true) {
        this.isMove = move;
    }

    public changeMoveData(moveData: EnemyMoveData) {
        this.moveData = moveData;
    }
    private isBreath: boolean = false;
    private initScale: number = null;
    public setBreath() {
        this.isBreath = true;
        // this.breath()
    }
    private scaleWay = 1;
    private breath() {
        if (!this.isBreath) return;
        let minScale = 0.7 * this.initScale;
        let maxScale = 1.3 * this.initScale;
        // this.player.levelbase.scheduleAny(() => {
        if (!this.isBreath) return;
        if (this.scale > maxScale) {
            this.scaleWay = -1;
        }
        else if (this.scale < minScale) {
            this.scaleWay = 1;
        }
        this.scale += this.scaleWay * cc.director.getDeltaTime() / 1;
        // })
    }
    private targetCall: () => void = null
    public setTargetCallBack(call: () => void, isDelCall: boolean = true) {
        this.targetCall = () => {
            call && call()
            if (isDelCall) this.targetCall = null;
        };
    }

    private _scale: number = 1;
    set scale(v: number) {
        this._scale = v;

        this.lab.fontSize = (this._scale * this.scaleLabFontSize) || 1;
        this.lab.lineHeight = (this._scale * this.scaleLabFontSize) || 1;
        this.scheduleOnce(() => {
            this.node.width = this.lab.node.width;
            this.node.height = this.lab.node.height;
            this.node.getComponent(cc.BoxCollider).size.width = this.node.width -20;
            this.node.getComponent(cc.BoxCollider).size.height = this.node.height -20;
        })
    }
    get scale() {
        return this._scale;
    }
}
export interface EnemyMoveData {
    /**移动方式 */
    moveType: EnemyMoveType,
    /**移动速度 */
    moveSpeed: number,
    /**初始位置 */
    startPos: cc.Vec2,
    /**当前移动方式为直行时 移动方向 */
    moveDirection?: cc.Vec2
    /**当前移动方式为范围追踪时 判断范围 在横坐标跟纵坐标相距在此区间时 【以自身为长方形范围内】*/
    attRanglePos?: cc.Vec2
    /**当前移动方式为目标到达时 目标位置 */
    targetPos?: cc.Vec2[]

    /**当前移动方式为范围内随机移动 随机范围 */
    rangleV4?: cc.Vec4

    /**当前移动方式为贝塞尔曲线时 起中终三个点 */
    routePosList?: cc.Vec2[]
    /**当前移动方式为贝塞尔曲线时 总移动时间 */
    beiziTime?: number
}
export enum EnemyMoveType {
    /**直线追踪 */
    one,
    /**直行 */
    two,
    /**范围追踪 */
    three,
    /**目标到达 */
    four,
    /**范围内移动 */
    five,
    /**贝塞尔曲线 */
    six
}